package com.wxmit.starter.xfile.core.serialize;

import cn.hutool.core.collection.CollectionUtil;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import lombok.extern.slf4j.Slf4j;
import com.wxmit.starter.xfile.config.ShushanFileConfig;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 反序列化
 * @author huangxiong
 * @date 2021/6/19
 */
@Slf4j
public class FileRealmDeserializer extends JsonDeserializer<Object> {

    @Override
    public Object deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {

        JsonToken currentToken = jsonParser.getCurrentToken();
        // VALUE_STRING,START_ARRAY
        switch (currentToken){
            case VALUE_STRING:
                return  delFilePrefix(jsonParser.getText());
            case START_ARRAY:
                List<String> list = jsonParser.readValueAs(List.class);
                if(!CollectionUtil.isEmpty(list)){
                    return list.stream().map(this::delFilePrefix).collect(Collectors.toList());
                }
                return list;
            default:
        }
        return jsonParser.readValueAs(String.class);
    }

    public String delFilePrefix(String val){
       return ShushanFileConfig.delFilePrefix(val);
    }
}
